#include "LINFOScreen.h"
#include "MAHeaders.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/ScrollPane.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"
#include "../Utilities/Convert.h"

LINFOScreen::LINFOScreen(){
	mProcessor = new LINFOProcessor();

	isGetData = false;

	mLayout = new Layout(0, 0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight(), NULL, 1, 3);

	//Create Main Title
	mTitle = new Label(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTBAR, mLayout);
	mTitle->setCaption("Luckyzone");
	mTitle->setHorizontalAlignment(Label::HA_CENTER);
	mTitle->setVerticalAlignment(Label::VA_CENTER);
	mTitle->setFont(APPCONTROLLER->getFont("default"));
	mTitle->setDrawBackground(true);
	mTitle->setPaddingLeft(0);
	mTitle->setPaddingRight(0);
	mTitle->setSkin(APPCONTROLLER->getSkin("title"));
	mTitle->update(); //Calculate the correct size

	//Create SoftKey Bar
	softKeys = createSoftKeyBar(HEIGHTBAR, getStringNotIterative(L_CANCEL) , getStringNotIterative(L_BACK));

	//Create panel for content
	panel = new ScrollPane(0,0, APPCONTROLLER->getScreenWidth(), APPCONTROLLER->getScreenHeight()-softKeys->getHeight()- mTitle->getHeight(), mLayout, 1, 1);
	panel->setDrawBackground(true);
	panel->setBackgroundColor(0);
	//panel->setSkin(APPCONTROLLER->getSkin("default"));
	mResult = new Label(0, 0, APPCONTROLLER->getScreenWidth(), 32, NULL, "",0, APPCONTROLLER->getFont("default"));
	mResult->setAutoSizeY(true);
	mResult->setMultiLine(true);
	panel->add(mResult);

	mLayout->add(softKeys);
	this->setMain(mLayout);
}

LINFOScreen::~LINFOScreen(){
	delete mLayout;
}

void LINFOScreen::keyPressEvent(int keyCode, int nativeCode){
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			panel->scrollDown();
			break;
		case MAK_DOWN:
		case MAK_8:
			panel->scrollUp();
			break;
		case MAK_SOFTRIGHT:
			APPCONTROLLER->back();
			break;
		case MAK_SOFTLEFT:;
			Environment::getEnvironment().removeTimer(this);
			APPCONTROLLER->show("LRESIndex");
			break;
	}
}

void LINFOScreen::pointerReleaseEvent(MAPoint2d point){

}

void LINFOScreen::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (panel->contains(p)){

	}
	else if (softKeys->contains(p)){
		if(softKeys->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (softKeys->getChildren()[1]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void LINFOScreen::itemReceived(LINFOItem& item){

	if ((strncmp(item.isnew.c_str(), "0", 1) == 0) && (strncmp(item.isend.c_str(), "1", 1) == 0)){
		isWaitingTime = true;
		/*if (isWaitingTime)
		{
			lprintfln("waiting time");
			Environment::getEnvironment().addTimer(this, Convert().toInt(item.remaintime), 1);
			isWaitingTime = false;
		}*/
	}else if ((strncmp(item.isnew.c_str(), "1", 1) == 0)&& (strncmp(item.isend.c_str(), "1", 1) == 0)){
		if (!isLiveTime){
			lprintfln("remove waiting timer, live time");
			Environment::getEnvironment().removeTimer(this);
			Environment::getEnvironment().addTimer(this, LIVEFRAMES, -1);
			isLiveTime = true;
		}
	}else if ((strncmp(item.isnew.c_str(), "0", 1) == 0) && (strncmp(item.isend.c_str(), "0", 1) == 0)){
		if (isLiveTime){
			lprintfln("remove live timer");
			Environment::getEnvironment().removeTimer(this);
		}
	}
	mResult->setCaption(item.result);
	mResult->update();
	panel->reset();
}

void LINFOScreen::processingComplete(){
	isGetData = true;
	lprintfln("process LINFO complete");
}

void LINFOScreen::show(){
	if (!isGetData){
		String url = APPCONTROLLER->getUrl();
		isWaitingTime = true;
		isLiveTime = false;
		mProcessor->addRequest(url, companyId, this);
	}
	Screen::show();
}

void LINFOScreen::setCompanyId(String& value){
	companyId = value;
	mResult->setCaption("");
	mResult->update();
	panel->reset();
}

void LINFOScreen::setIsGetData(bool value){
	isGetData = value;
}

void LINFOScreen::setCompanyName(const String& value){
	mTitle->setCaption(value);
	mTitle->update();
}

void LINFOScreen::runTimerEvent(){
	lprintfln("goi dich vu");
	maVibrate(FLASHSCREEN);
	mProcessor->addRequest(APPCONTROLLER->getUrl(), companyId, this);
}
